import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2022-05-2
 * Time: 下午 19:34
 */
public class Link {

    static class NodeList {
        private int val;
        private NodeList next;

        public NodeList(int val) {
            this.val = val;
        }
    }

    private NodeList head;

    public void CreateList() {
        NodeList nodeList1 = new NodeList(15);
        NodeList nodeList2 = new NodeList(25);
        NodeList nodeList3 = new NodeList(43);
        NodeList nodeList4 = new NodeList(56);
        NodeList nodeList5 = new NodeList(78);

        nodeList1.next = nodeList2;
        nodeList2.next = nodeList3;
        nodeList3.next = nodeList4;
        nodeList4.next = nodeList5;

        head = nodeList1;
    }

    public void Display() {
        NodeList node = head;

        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data) {
        NodeList node = this.head;
        NodeList nodeList = new NodeList(data);

        nodeList.next = node;
        this.head = nodeList;

    }

    //尾插法
    public void addLast(int data) {
        NodeList node = this.head;
        NodeList nodeList = new NodeList(data);

        while(node.next != null) {
            node = node.next;
        }

        node.next = nodeList;

    }

    private  NodeList findSub(int index) {
        NodeList node1 = head;

        while(index-1 > 0) {
            node1 = node1.next;
            index--;
        }
        return node1;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        NodeList nodeList = new NodeList(data);

        if(!(index >= 0 && index <= size())) {
            throw new AddIndexRuntimeExecption("下标越界:");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }

        NodeList node = findSub(index);
        nodeList.next = node.next;
        node.next = nodeList;

    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        NodeList node = this.head;

        while(node != null) {
            if(node.val == key) {
                return true;
            }
            node = node.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head == null) {
            return;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }

        NodeList node = this.head;
        while(node.next!= null) {
            if(node.next.val == key) {
                node =node.next.next;
            }
            node = node.next;
        }

    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {

    }

    //得到单链表的长度
    public int size() {
       NodeList node = this.head;
       int count = 0;

       while(node!= null) {
           count++;
           node = node.next;
       }
       return count;
    }


    public void clear() {

        head.next = null;
        this.head = null;
    }
}

